package com.bysj.money_diary;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

@SuppressLint("NewApi")
public class Gaishezhi extends Fragment implements OnClickListener{
	private static final String TAG = null;
	private View view;
	Button gaishezhi_relogin,gaishezhi_submit;
	EditText gaishezhi_username,gaishezhi_password_old,gaishezhi_password_new,gaishezhi_password_new1;
	Data dataHelper;
	
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.activity_gaishezhi, container, false);
		dataHelper = new Data(this.getActivity());
		initView();
		return view;
	}


	private void initView() {
		gaishezhi_username=(EditText)view.findViewById(R.id.gaishezhi_username);
		gaishezhi_password_old=(EditText)view.findViewById(R.id.gaishezhi_password_old);
		gaishezhi_password_new=(EditText)view.findViewById(R.id.gaishezhi_password_new);
		gaishezhi_password_new1=(EditText)view.findViewById(R.id.gaishezhi_password_new1);
		gaishezhi_relogin=(Button)view.findViewById(R.id.gaishezhi_relogin);
		gaishezhi_submit=(Button)view.findViewById(R.id.gaishezhi_submit);
		
		gaishezhi_relogin.setOnClickListener(this);
		gaishezhi_submit.setOnClickListener(this);
	}


	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.gaishezhi_relogin:
			gaishezhi_relogin.setBackgroundResource(R.drawable.main_button_background);
			Intent intent=new Intent();
			intent.setClass(this.getActivity(), LoginActivity.class);
			startActivity(intent);
			gaishezhi_relogin.setBackgroundResource(R.drawable.border_item1);
			break;
		case R.id.gaishezhi_submit:
			resetpwd_check();
			break;
		default:
		
		}
	}


	private void resetpwd_check() {
		if (isUserNameAndPwdValid()){
			String userName = gaishezhi_username.getText().toString().trim(); 
			String userPwd_old = gaishezhi_password_old.getText().toString().trim();
            String userPwd_new = gaishezhi_password_new.getText().toString().trim();  
            String userPwdCheck = gaishezhi_password_new1.getText().toString().trim();  
            if(!dataHelper.haveUserInfo(userName)){  
                Toast.makeText(this.getActivity(), getString(R.string.name_not_exist, userName),Toast.LENGTH_SHORT).show();  
                return ;  
            } 
            int result=dataHelper.UserbyNameAndPwd(userName, userPwd_old); 
            if(result==1){                                              
                if(userPwd_new.equals(userPwdCheck)==false){            
                    Toast.makeText(this.getActivity(), getString(R.string.pwd_not_the_same),Toast.LENGTH_SHORT).show();  
                    return ;  
                } else {  
        			User user = new User();//实例化
        			int flag = dataHelper.UpdateUserInfo(user,userPwd_new, userName);     
        			Log.i(TAG,"UpdateUserInfo , flag="+flag); 
                    if (flag < 1) {  
                        Toast.makeText(this.getActivity(), getString(R.string.setpwd_fail),Toast.LENGTH_SHORT).show();  
                    }else{  
                        Toast.makeText(this.getActivity(), getString(R.string.setpwd_success),Toast.LENGTH_SHORT).show();    
                        Intent intent1 = new Intent();
            			intent1.setClass(this.getActivity(), LoginActivity.class);  
            			startActivity(intent1);  
                    }  
                }  
            }else if(result==0){                                         
                Toast.makeText(this.getActivity(), getString(R.string.oldpaw_fail),Toast.LENGTH_SHORT).show();  
            }
		}	
		
	}


	private boolean isUserNameAndPwdValid() {
		if (gaishezhi_username.getText().toString().trim().equals("")) {  
            Toast.makeText(this.getActivity(), getString(R.string.account_empty),Toast.LENGTH_SHORT).show();  
            return false;  
        }else if (gaishezhi_password_old.getText().toString().trim().equals("")){
		 Toast.makeText(this.getActivity(), getString(R.string.opas_check_empty),Toast.LENGTH_SHORT).show();  
         return false;
        }else if (gaishezhi_password_new.getText().toString().trim().equals("")){
		 Toast.makeText(this.getActivity(), getString(R.string.pwd_new_empty),Toast.LENGTH_SHORT).show();  
         return false;
        } else if (gaishezhi_password_new1.getText().toString().trim().equals("")) {  
            Toast.makeText(this.getActivity(), getString(R.string.pwd_check_empty),Toast.LENGTH_SHORT).show();  
            return false;  
        }
        return true;
	}




	
}
